import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.UpdateOptions;
import org.bson.Document;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GetHTML {
    public static void main(String[] args) throws IOException {
        //数据提供关键审计事项2020表
        MongoCollection<org.bson.Document> sourse = new MongoClient("192.168.1.174", 27017).getDatabase("KAMD").getCollection("关键审计事项2020");
        MongoCollection<org.bson.Document> target = new MongoClient("192.168.1.174", 27017).getDatabase("KAMD").getCollection("KAMD_Date_2020");
        File f = new File("E:\\年报\\2020年年报HTML\\HTML");
        FindIterable<org.bson.Document> documents = sourse.find().sort(new org.bson.Document("_id", 1));
        documents.noCursorTimeout(true);
        int sum = 0;
        File[] f_all = f.listFiles();
        for (File file : f_all) {
            System.out.println(file);
            String Scode = file.getName().split("_")[0];
            String Name = file.getName().split("_")[2];
            String type = "";
            String type_content = "";
            String html = getHtmlByPageName2(file);
            org.jsoup.nodes.Document doc = Jsoup.parse(html);
            Elements table = doc.select("body > table");
            List<org.bson.Document> list = new ArrayList();
            org.bson.Document document1 = new org.bson.Document();
            if (!doc.text().contains("关键审计事项")) {
                System.out.println(Scode);
                System.out.println(Name);
                System.out.println(type_content);
                document1.append("Scode", Scode);
                document1.append("Name", Name);
                document1.append("type_content", "");
                sum++;
                continue;
            }
            StringBuffer stringBuffer = new StringBuffer();
            for (Element element : table) {
                if (element.text().contains("关键审计事项")) {
                    type = element.select("tbody > tr:nth-child(1) > td:nth-child(1)").text();//类型
                    if ("关键审计事项".equals(type))
                        type = "";
                    type_content = element.select("tbody > tr:nth-last-child(1) > td:nth-child(1)").text();//类型内容
                    System.out.println(Scode);
                    System.out.println(Name);
                    System.out.println(type_content);
                    stringBuffer.append(type_content);
                    document1.append("Scode", Scode);
                    document1.append("Name", Name);
                    document1.append("type_content", stringBuffer.toString());
                    sum++;
                    list.add(document1);
                }
            }
            if (list.size() == 0) {
                String text = doc.text();
                String pattern = "[一二三四五六七八九十][）)]?[、]?[  ]*关键审计事项";
                // 创建 Pattern 对象
                Pattern r = Pattern.compile(pattern);
                // 现在创建 matcher 对象
                int start = 0;
                Matcher m = r.matcher(text);
                if (m.find()) {
                    System.out.println("Found value: " + m.group(0));
                    start = text.indexOf(m.group(0));
                } else {
                    System.out.println("正则有问题，请修改！");
                    document1.append("Scode", Scode);
                    document1.append("Name", Name);
                    document1.append("type_content", "");
                    target.updateOne(new BasicDBObject("_id", Scode), new BasicDBObject("$set", document1), (new UpdateOptions()).upsert(true));
                    continue;
                }
                int end = text.lastIndexOf("关键审计事项");
                type_content = text.substring(start, end + 6);
                System.out.println(Scode);
                System.out.println(Name);
                System.out.println(type_content);
                document1.append("Scode", Scode);
                document1.append("Name", Name);
                document1.append("type_content", type_content);
                sum++;
                list.add(document1);
            }
            target.updateOne(new BasicDBObject("_id", Scode), new BasicDBObject("$set", document1), (new UpdateOptions()).upsert(true));
        }
    }
    public static String getHtmlByPageName2(File file) throws IOException {
        InputStream is = new FileInputStream(file);
        // 返回读取指定资源的输入流
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String s = "";
        StringBuffer sb = new StringBuffer();
        while ((s = br.readLine()) != null) {
            sb.append(s).append("\n");
        }
        return sb.toString();
    }
}
